package com.netmi.myjectpack.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;
import androidx.fragment.app.Fragment;

import com.netmi.myjectpack.R;
import com.netmi.myjectpack.bean.ObservableGoods;
import com.netmi.myjectpack.bean.UserInfo;
import com.netmi.myjectpack.databinding.FragmentDatabind1Binding;
import com.netmi.myjectpack.viewmodel.MyBaseLiveDataViewModel;

import java.util.Random;

/**
 * 类描述：
 * 创建人：tgw
 * 创建时间：2020/8/6
 * 修改备注：
 */
public class DataBindFragment extends Fragment {
    public static final String TAG = DataBindFragment.class.getName();

    private FragmentDatabind1Binding mBinding;
    private MyBaseLiveDataViewModel model;

    private UserInfo userInfo;
    private ObservableGoods goods;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mBinding == null)
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_databind_1, container, false);
        return mBinding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        userInfo = new UserInfo();
        userInfo.setNickname("tgw单向");
        userInfo.setPhone("电话号码123456");
        mBinding.setUserInfo(userInfo);

        goods = new ObservableGoods("code", "hi", 23,"地址哈哈");
        mBinding.setGoods(goods);//这里赋值goods的值，在xml中使用 同一个字段 即可实现双向绑定

        mBinding.setDatabind(DataBindFragment.this);

    }

    public void doClick(View view){
        goods.getName();
        ObservableField<String> name = new ObservableField<>("改变值不赋值,值却改变"+new Random().nextInt(100));
        goods.setName(name);
//        mBinding.setGoods(goods);//databind赋值

    }
}
